package com.crb.crm.manage.message.dingding;

import com.crb.crm.manage.dao.biz.TemplateInfoMapper;
import com.crb.crm.manage.entity.biz.TemplateDTO;
import com.crb.crm.manage.message.token.TokenHandlerTools;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Package: com.gulimall.thrid.party.dingding
 * Description：釘釘自定義模板
 * Author: huangjian
 * Date:  2020/7/1 14:06
 * Modified By:
 */
@Component
public class DingdingMessageTemplate {
    private static final String BR = "\n";
    private static final String SIGNATURE = "${signature}";
    @Resource
    private TemplateInfoMapper templateInfoMapper;

    public  String formatVars(String templateCode, Map<String, String> param, String signName) {
        TemplateDTO template = templateInfoMapper.getByCode(templateCode);
        if (template == null) {
            throw new RuntimeException("找不到對應模板【" + templateCode + "】，請檢查");
        }
        if (param == null) {
            param = Maps.newHashMap();
        }
        param.put("signature", signName);
        StringBuilder stringBuilder=new StringBuilder(template.getContent());
        stringBuilder.append(BR)
                .append(SIGNATURE);
        return TokenHandlerTools.parse(stringBuilder.toString(), param);
    }

}
